/*
 * 门诊所有刷卡之后的事件
 */

var cardMap; //查询出来的卡信息
//1 挂号页面刷卡按钮两种方式，1 弹出卡号输入框；2 不弹出卡号输入框直接获取卡号
function readAllCard(){
	 if(valueArr["p_100_105002_415"] == "1"){ //1 弹出卡号输入框
		 readMemberCard(3); 
		
     }else{  //2 不弹出卡号输入框直接获取卡号
         $.when(  
        	 getMemberCardNumToDate()  //卡号
         ).done(function (cardObj) {
        	 $.when( 
        			 getMemberCardData(cardObj.cardNo) //检索病人信息  
                 ).done(function (json) { //病人信息
                	//检索病人信息  
                	 json = $.extend(json,cardObj);
                	 if (json.vcb12 == 2)
     				{
     					queryPatientInfo(json); //病人信息赋值
     				}
                 });
        	
         });
        	
     }
	
}
//弹出窗口三种刷卡方式（按钮、文本框、confirm输入框）
//弹出刷卡页面读取卡信息
function getDatByCardNum(cardNo){
	$.when(
			 getMemberCardData(cardNo)
     ).done(function (data) {
    	 //检索病人信息
    	 if (json.vcb12 == 2)
			{
				queryPatientInfo(json);
			}
        	
     });
}
//挂号页面保存挂号弹出刷卡页面进行刷卡交易
function saveGuhaoCharge(){
	 if(valueArr["p_100_105002_415"] == "1"){ //1 弹出卡号输入框
		 readMemberCard(4);
		
    }else{  //2 不弹出卡号输入框直接获取卡号
        $.when(  
        	getMemberCardNumToDate()  //卡号
        ).done(function (cardObj) {
       	 $.when( 
       			 getMemberCardData(cardObj.cardNo) //检索病人信息  
                ).done(function (json) { //病人信息
               	 if (json.vcb12 == 2)
    				{
               		 	cardMap = json;
               		 	guahaoYibaoJiaoyi(json); //开始交易  
    				}
                });
       	
        });
       	
    }
}
var jiaoyi_window
function guahaoYibaoJiaoyi(data)
{
	var ifShowPassInput = memberSettingData['116001_1_90'];
	var data = {
		ifShowPassInput:ifShowPassInput
	}
	laytplUrl(myurl + "/module/guahao_jiaoyi_html.html",data, function(msg, html) {
		 jiaoyi_window = layer.open({
			type : 1,
			title : "交易窗口",
			area : [ "400px", "450px" ],
			shade : 0.1,
			content : html,
			closeBtn : 1,
			success : function(layero, index)
			{
				debugger;
				
				$("#yukou_e").val(totalVbi);
				$("#jifen").val(cardMap.vbu17);
				$("#kahao").val(cardMap.vcb04);
				$("#yu_e").val(cardMap.vbu14);
				$("#vbu09").focus();
				// 密码校验
				$("#vbu09").val("");
			},
			cancel:function()
			{
				totalVbi = 0;
			}
		})
		
	});
}
function removeAction_jiaoyi(){
	layer.close(jiaoyi_window);
}
//键盘输入密码点击确认交易事件
function vbu09_chareCardUpdate(that){
	 $(that).attr("disabled",true);
	var cardPwd = $("#vbu09").val();
	checkCardPassWord(cardPwd);
	 $(that).removeAttr("disabled");
}

//校验密码是否正确
function checkCardPassWord(cardPwd){
	
	$.ajax({
 		url: rootUrl + "/clidoctor/getMD5PassWord",
 		type: "GET",
 		dataType: 'json',
 		data: {password:cardPwd},//$("#vbu09").val()界面输入传值
 		success:function(json){
 		if(cardMap.vbu09 != json.pass && cardPwd != "d41d8cd98f00b204e9800998ecf8427e")
 		{
 			layer.alert("密码错误，请重新输入");
 		}
 		else if(cardMap.vbu09 == json.pass)
 		{
 			skjy();
 		}
 	}});
}

/**
 * 读卡器输入密码点击额确认挂号交易的事件
 */
function guahao_chareCardUpdate(that)
{
	 $(that).attr("disabled",true);
	 if(cardMap.vbu09 == "d41d8cd98f00b204e9800998ecf8427e")
	 {
		 skjy(); 
	 }
	 else
	 {
		 var orderStrUrl = hoinsurance+'/GetRequest?comid=3&comstr=pwd&vctype=4';
		    $.ajax({ url: orderStrUrl,
		         dataType: 'jsonp',
		         jsonp: "callback",
		         jsonpCallback: "success_jsonpCallback",
		         success: function (data) {
		        	 $(that).removeAttr("disabled");
		        		 if(data.result=="success")
		        		 {
		        			  if(data.cardPwd)
		        			  {
		        				  checkCardPassWord(data.cardPwd);
		        			  }
		        		 }
		        		 else
		        		 {
		        			 layer.alert(data.cardPwd, {icon:2}); 
		        			 $(that).removeAttr("disabled");
		        		}
		         }, error: function (xhr) 
		        { 
		    	   	 layer.alert("请检查就诊卡服务是否启动", {icon:2});
		    	   	$(that).removeAttr("disabled");
		        }
		    })
	 }
}
//刷卡成功之后展示病人信息
function showCardSuccessMess(json){
	$("#swing_card .swing-card-info").removeClass("hidden");
	$("#name").text(json.vaa05);
	$("#cardNum").text(json.vcb04);
	$("#cardIssTime").text(moment(json.vcb16).format("YYYY-MM-DD HH:mm:ss"));
	$("#validTime").text(moment(json.vcb17).format("YYYY-MM-DD"));
	$("#accountNum").text(json.vbu03);
	$("#balance").text(json.vbu14);
	$("#plase_shuaKa").hide();
}
//刷卡之后业务分支
//挂号页面刷医保卡检索病人
function MedicalInsuranceCard(){
	debugger
	$.when(
			getMedicalInsuranceData()
        ).done(function (data) {
			debugger
        	 if(memberSettingData['116001_1_79']==1){
		    	 if(data["root"]["iar1"]["ie"]["iak05"]){
		    		//医保卡当就诊卡用,查询病人该医院的信息
    				 InsuranceAsHospitalCard(data);
		    	 }
			 }else{
				//医保查询人员信息
				 InsuranceCardValue(data);
			 }
    });
}
//挂号保存根据医保卡加载就诊卡信息，输入密码扣费
function afterInsuranceCharge(){
	debugger
	$.when(
			getMedicalInsuranceData()
        ).done(function (data) {
			guahaoYibaoJiaoyi(data);
        	
    });
}

function afterSuccessCardItem(obj){
	debugger;
	var type = obj.type;
	if (type == 1)
	{
		searchShuaPromVal(obj);
		$("#shuaka_queding").on("click",function(){
			cardSearch();
		});
	}
	else if (type == 2)//发送扣费交易
	{
		searchShuaPromVal_jiaoyi(obj);
	}
	else if (type == 3)//刷卡检索病人信息
	{
		searchShuaPromVal_guahao(obj);
	}
	else if (type == 4)//挂号扣费
	{
		searchShuaPromVal_guahaocharge(obj);
	}
	else if (type == 5)//诊金扣费
	{
		searchShuaPromVal_zhenjincharge(obj);
	}
	layer.close(obj.layer);
}



/*  @param indexCardNumber type == 1*/
function searchShuaPromVal(obj)
{
	var cardNumber = obj.cardNumber;
	ajaxGet(memberUrl + "/member/memberManger/queryMemberByNumber",
	{
	cardNumber : cardNumber,
	jsessionids : $.cookie('jsessionids')
	}, function(json)
	{
		if (json)
		{
			if (json.vcb12 == 2)
			{
				if(memberSettingData['116001_1_17']=="1"){
                    pleaseProPwd(json,indexCardNumber);
                }else{
                	initDataHuiYuan(json);
                }
			}
		}
		else
		{
			layer.alert("该卡号不存在或没有关联的会员卡！",{icon : 0});
		}
	});
}
/* 发送扣费交易 ------------------- @param indexCardNumber type == 2*/

function searchShuaPromVal_jiaoyi(obj)
{
	var cardNumber = obj.cardNumber;
	if (cardNumber)
	{
		ajaxGet(memberUrl + "/member/memberManger/queryMemberByNumber",
		{
		cardNumber : cardNumber,
		jsessionids : $.cookie('jsessionids')
		}, function(json)
		{
			if (json)
			{
				if (json.vcb12 == 2)
				{
					cardMap = json;
					$("#swing_card .swing-card-point").addClass("hidden");
					$("#swing_card .swing-card-info").removeClass("hidden");
					jiaoyi();
				}
				
			}
			else
			{
				layer.alert("会员卡号不存在或没有关联的会员卡！",{icon : 0});
			}
		});
	}
	else
	{
		layer.msg('卡号不能为空！',{icon : 0});
	}
}

/* 挂号界面刷卡查询病人信息 @param indexCardNumber type == 3*/
function searchShuaPromVal_guahao(obj)
{
	var cardNumber = obj.cardNumber;
	if (cardNumber)
	{
		ajaxGet(memberUrl + "/member/memberManger/queryMemberByNumber",
		{
		cardNumber : cardNumber,
		jsessionids : $.cookie('jsessionids')
		}, function(json)
		{
			if (json)
			{
				if (json.vcb12 == 2)
				{
					queryPatientInfo(json);
				}
			}
			else
			{
				layer.alert("会员卡号不存在或没有关联的会员卡！",{icon : 0});
			}
		});
	}
	else
	{
		layer.msg('卡号不能为空！',{icon : 0});
	}
}
//医院病人信息赋值
function HospitalCardValue(list){
	$('#gh_vaa05').val(list.vaa05);
	$("#vaa10").val(list.vaa10);
	var vaa12Value = list.vaa12;
	if (vaa12Value != "undefined")
	{
		$('#vaa12').val(moment(vaa12Value).format("YYYY-MM-DD HH:mm:ss"));
	}
	else
	{
		$('#vaa12').val(moment(new Date()).format("YYYY-MM-DD HH:mm:ss"));
	}
	$('#vaa02').val(list.vaa02);
	$("#gh_vaa01").val(list.vaa01);
	$("#vaa03").val(list.vaa03);
	$("#vaa03_1").val(list.vaa03);
	$('#vaa15').val(list.vaa15);
	$('#vaa35').val(list.vaa35);
	$("#vac19").val(list.vaa33);
	$('#bep06b').val(list.bep06b == "null" ? 0 : list.bep06b);
	$('#vac57').val(list.vac57);
	$('#vac46').val(list.vac46);
	$('#ack01').val(list.ack01);
	$('#vac16').val(list.vac16);
	$('#abc02').val(list.abc02);
	if (list.aau01)
	{
		$('#aau01').val(list.aau01).trigger("change");
	}
	$('#vbu01').val(list.vbu01);
	$('#vbu14').val(list.vbu14 == "undefined" ? 0 : list.vbu14);
	$('#vbu15').val(list.vbu15 == "undefined" ? 0 : list.vbu15);
	$('#vbu30').val(list.vbu30 == "undefined" ? 0 : list.vbu30);
	if (list.bdp02)
	{
		$('#bdp02').val(list.bdp02).trigger("change");
	}
}
//获取病人信息
function queryPatientInfo(cardMap)
{
	
	/*if(cardMap.bck01a)
	  {
		  $("#vac06_1").iCheck("check");
		  $("#vac06").val(1);
	  }
	  else
	  {
		  $("#vac06_2").iCheck("check");
		  $("#vac06").val(0);
	  }*/
	$('#cardType').val(cardMap.cardType);//卡类型
//	$('#cardType').val(1);//卡类型
	$('#gh_vaa05').val(cardMap.vaa05);
	$('#gh_vaa01').val(cardMap.vaa01);
	$('#vaa02').val("");
	$("#vaa10").val(cardMap.vaa10);
	$("#abw02").val(cardMap.abw02);
	$("#gh_vaa01").val(cardMap.vaa01);
	if(cardMap.vaa03){
		$("#vaa03").val(cardMap.vaa03);
		$("#vaa03_1").val(cardMap.vaa03);
	}
	
	$('#vaa15').val(cardMap.vaa15);
	$('#vaa35').val(cardMap.vaa35);
	$('#bep06b').val(cardMap.bep06b == "null" ? 0 : cardMap.bep06b);
	$('#vac57').val(cardMap.vac57);
	$('#vac46').val(cardMap.vac46);
	$('#ack01').val(cardMap.ack01);
	$('#vac16').val(cardMap.vac16);
	$('#abc02').val(cardMap.abc02);
	$('#aau01').val(cardMap.aau01);
	var vaa12Value = cardMap.vaa12;
	$('#vbu01').val(0);
	$('#vbu14').val("");
	$('#vbu15').val("");
	$('#vbu30').val("");
	if (vaa12Value)
	{
		$('#vaa12').val(moment(vaa12Value).format("YYYY-MM-DD HH:mm:ss"));
	}
	else
	{
		$('#vaa12').val(moment(new Date()).format("YYYY-MM-DD HH:mm:ss"));
	}
	$('#bdp02').val(cardMap.bdp02).trigger("change");
	if(cardMap.Sex_Code){
		$("#abw01").val(cardMap.Sex_Code).trigger("change");
	}
	if(cardMap.abw01){
		$("#abw01").val(cardMap.abw01).trigger("change");
	}
}

//身份证信息赋值
function dataFromIdentityCard(cardObj){
	 $("#gh_vaa05").val(cardObj.Name);
	 $("#abw01").val(cardObj.Sex_Code).trigger("change");
	 $("#vaa10").val(cardObj.Age);
	 $("#aau01").val(cardObj.Age_Code).trigger("change");
     $("#vaa12").val(moment(cardObj.BirthDate).format("YYYY-MM-DD HH:mm:ss"));
	 $("#abq02").val(cardObj.Nation_Name).trigger("change");
	 $("#vaa33").val(cardObj.Address);
	 $("#vaa15").val(cardObj.CardNo);
}
/* 挂号扣费    ---------------- @param indexCardNumber type == 4*/
function searchShuaPromVal_guahaocharge(obj)
{
	var cardNumber = obj.cardNumber;
	if (cardNumber)
	{
		ajaxGet(memberUrl + "/member/memberManger/queryMemberByNumber",
				{
			cardNumber : cardNumber,
			jsessionids : $.cookie('jsessionids')
				}, function(json)
				{
					if (json)
					{
						if (json.vcb12 == 2)
						{
							cardMap = json;
							$("#swing_card .swing-card-point").addClass("hidden");
							$("#swing_card .swing-card-info").removeClass("hidden");
							guahao_jiaoyi();
						}
				
					}
					else
					{
						layer.alert("会员卡号不存在或没有关联的会员卡！",{icon : 0});
					}
				});
	}
	else
	{
		layer.msg('卡号不能为空！',{icon : 0});
	}
}
/**
 * 挂号交易
 */
function guahao_jiaoyi()
{
	
	var ifShowPassInput = memberSettingData['116001_1_90'];
	var data = {
		ifShowPassInput:ifShowPassInput
	}
	laytplUrl(myurl + "/module/guahao_jiaoyi_html.html",data, function(msg, html) {
		 jiaoyi_window = layer.open({
			type : 1,
			title : "交易窗口",
			area : [ "400px", "450px" ],
			shade : 0.1,
			content : html,
			closeBtn : 1,
			success : function(layero, index)
			{
				debugger;
				// 会员卡消费信息赋值
				$("#yukou_e").val(totalVbi);
				$("#jifen").val(cardMap.vbu17);
				$("#kahao").val(cardMap.vcb04);
				$("#yu_e").val(cardMap.vbu14);
				$("#vbu09").focus();
				// 密码校验
				$("#vbu09").val("");
			},
			cancel:function()
			{
				totalVbi = 0;
			}
		})
		
	});
}
/* 刷卡诊金扣费    ----------- @param indexCardNumber type == 5*/
function searchShuaPromVal_zhenjincharge(obj)
{
	var cardNumber = obj.cardNumber;
	if (cardNumber)
	{
		ajaxGet(memberUrl + "/member/memberManger/queryMemberByNumber",
				{
			cardNumber : cardNumber,
			jsessionids : $.cookie('jsessionids')
				}, function(json)
				{
					if (json)
					{
						if (json.vcb12 == 2)
						{
							cardMap = json;
							$("#swing_card .swing-card-point").addClass("hidden");
							$("#swing_card .swing-card-info").removeClass("hidden");
							zhenjin_jiaoyi();
						}
					}
					else
					{
						layer.alert("会员卡号不存在或没有关联的会员卡！",{icon : 0});
					}
				});
	}
	else
	{
		layer.msg('卡号不能为空！',{icon : 0});
	}
}
/*--------------------------医保卡病人赋值----------------------------------------------------*/
//医保查询病人赋值
function InsuranceCardValue(data){
	var list = data["root"]
	var pinfo = list["personinfo"]["ie"];
	var iar1 = list["iar1"]["ie"];
	//姓名
	 $("#gh_vaa05").val(pinfo["ptname"]);
	//性别
	 $("#abw01").val(pinfo["sex"]).trigger("change");
	 //年龄
	 $("#vaa10").val(pinfo["age"]);
	 //出生日期
	 if(pinfo["birthday"].indexOf(":") < 0){
		 pinfo["birthday"] = pinfo["birthday"]+" 00:00:00";
	 }
	 $("#vaa12").val(pinfo["birthday"]);
	 //身份证号
	 $("#vaa15").val(pinfo["idnumber"]);
	 //保险号
	 $("#iak05").val(iar1["iak05"]);
	 //保险卡号
	 $("#iak04").val(iar1["iak04"]);
	 //电脑号
	 $("#vaa17").val(iar1["iak06"]);
	 //医保金额
	 $("#iar33").val(iar1["iar33"]);
	 //单位编码
	 $("#iar12").val(iar1["iar12"]);
	 //医保单位名称
//	 $("#iar13").val(iar1["iar13"]);
     //病人类别
	 if(iar1["pttype"]){
		 $("#bdp02").val(iar1["pttype"]).trigger("change");
	 }
}

// 门诊首页刷就诊卡,检索候诊或接诊病人
function indexMemberCard(){
	debugger;
	 if(valueArr["p_100_105002_415"] == "1"){ //1 弹出卡号输入框
		 readMemberCard(6); 
		
     }else{  //2 不弹出卡号输入框直接获取卡号
         $.when(  
        	 getMemberCardNumToDate()  //卡号
         ).done(function (cardObj) {
        	 $.when( 
        			 getMemberCardData(cardObj.cardNo) //检索病人信息  
                 ).done(function (json) { //病人信息
                	//检索病人信息  
                	 json = $.extend(json,cardObj);
                	 debugger;
                	 if (json.vcb12 == 2)
     				{
     					getEmitPatient(json); //在候诊病人和接诊病人中搜索病人
     				}
                 });
        	
         });
        	
     }
}
// 门诊首页刷,检索候诊或接诊病人
function indexIdentityCard(){
	readIdentityCard("s");
}

function getEmitPatient(json){
	var key = json.vaa03; //根据门诊搜索
	var str = key.toUpperCase();
	reloadTable(str, houzhenTbleList, "patient_wait");
	reloadTable(str, myJieZhenTbleList, "myjiezhenTableList");
	reloadTable(str, benkeZhenTbleList, "jiezhenBenkeTable");
	reloadTable(str, wanZhenTbleList, "wanzhenBenkeTable");
	
	//查询到候诊病人自动新开
	if(valueArr['p_100_105002_106']=="1" && str!="")
	{
		 var rows = $("#patient_wait").jqGrid("getRowData");
		 var row1s = $("#myjiezhenTableList").jqGrid("getRowData");
		if(rows.length>0){
			$("#patient_wait").setSelection(1);
			$("#"+1,'#patient_wait').trigger("dblclick");
			
		}else if(row1s.length<1&&rows.length<1){ //打开挂号页面
			if (valueArr["p_100_105002_387"]=="1"){
				guahao('','s');
			}
		}
	}
}





